package com.cabbagetofu.crowd.controller;

import com.atguigu.crowd.constant.CrowdConstant;
import com.atguigu.crowd.until.ResultEntity;
import com.cabbagetofu.api.MySQLRemoteService;
import com.cabbagetofu.crowd.entity.vo.AddressVO;
import com.cabbagetofu.crowd.entity.vo.MemberLoginVO;
import com.cabbagetofu.crowd.entity.vo.OrderProjectVO;
import com.cabbagetofu.crowd.entity.vo.OrderVO;
import com.netflix.ribbon.proxy.annotation.Http;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class OrderController {

    @Autowired
    private MySQLRemoteService mySQLRemoteService;

    /**
     * 保存用户的地址信息至数据库中
     * @param addressVO
     * @param httpSession
     * @return
     */
    @RequestMapping("/save/address")
    public String saveAddress(AddressVO addressVO, HttpSession httpSession){

        // 通过远程方法保存地址信息
        ResultEntity<String> resultEntity = mySQLRemoteService.saveAddressRemote(addressVO);

        // 从session域得到当前的orderProjectVO
        OrderProjectVO orderProjectVO = (OrderProjectVO) httpSession.getAttribute("orderProjectVO");

        // 得到当前的回报数量
        Integer returnCount = orderProjectVO.getReturnCount();

        // 再次重定向到确认订单的页面（附带回报数量）
        return "redirect:http://localhost/order/confirm/order/" + returnCount;
    }


    /**
     * 确认当前用户要支持的回报项目，将回报数量更新至session域中，并查询当前用户关联的所有地址信息
     * @param returnCount
     * @param httpSession
     * @return
     */
    @RequestMapping("/confirm/order/{returnCount}")
    public String confirmOrderInfo(@PathVariable("returnCount") Integer  returnCount, HttpSession httpSession){

        // 从session域中取出orderProjectVO对象数据
        OrderProjectVO orderProjectVO = (OrderProjectVO) httpSession.getAttribute("orderProjectVO");
        // 将新的returnCount属性重新赋值
        orderProjectVO.setReturnCount(returnCount);
        // 更新session域中的orderProject对象
        httpSession.setAttribute("orderProjectVO", orderProjectVO);

        // 获取当前登录的用户数据
        MemberLoginVO memberLoginVO = (MemberLoginVO) httpSession.getAttribute(CrowdConstant.ATTR_NAME_LOGIN_MEMBER);
        // 获取当前登录用户的id
        Integer memberId = memberLoginVO.getId();
        // 根据当前用户的id查询该用户相关联的地址
        ResultEntity<List<AddressVO>> resultEntity = mySQLRemoteService.getAddressVORemote(memberId);

        // 如果查询成功，将地址信息放入session域中，让前端回显至页面上
        if (ResultEntity.SUCCESS.equals(resultEntity.getResult())){

            List<AddressVO> addresslist = resultEntity.getData();
            httpSession.setAttribute("addressVOList", addresslist);
        }

        return "confirm-order";
    }


    /**
     * 根据returnId查询相关的回报信息，并展示在“回报确认”页面
     * @param returnId
     * @param httpSession
     * @return
     */
    @RequestMapping("/confirm/return/info/{projectId}/{returnId}")
    public String confirmReturnInfo(@PathVariable("returnId") Integer returnId, HttpSession httpSession){

        ResultEntity<OrderProjectVO> resultEntity = mySQLRemoteService.getOrderProjectVORemote(returnId);

        if (ResultEntity.SUCCESS.equals(resultEntity.getResult())){

            httpSession.setAttribute("orderProjectVO", resultEntity.getData());
        }

        return "confirm-return";

    }

}
